/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.cmd;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.*;
import java.util.*;

import org.w3c.dom.*;

import self.awt.*;
import self.gee.*;
import self.gee.editor.*;

import self.amigo.io.*;

public class ClipboardPaste implements ICommand {
  private IDiagramEditor    editor;
  private boolean           isCopiedFromClipboard = false;
  private List              pastedElements = new ArrayList();
  private List              selections = new ArrayList();

  public ClipboardPaste( IDiagramEditor ed, List sel ) {
    editor = ed;
    selections.addAll( sel );
  }

  public void execute() {
    IDiagramLayer layer = editor.getSource().getContext();
    layer.beginUpdate();
    addPastedElementsIntoDiagram();
    layer.endUpdate();
    editor.getSource().redraw();
    editor.setSelectedElements( pastedElements );
    editor.inspectSelectedObjects( pastedElements );
  }

  public void undo() {
    IDiagramLayer layer = editor.getSource().getContext();
    layer.beginUpdate();
    removePastedElementsFromDiagram();
    layer.endUpdate();
    editor.getSource().redraw();
    editor.setSelectedElements( selections );
    editor.inspectSelectedObjects( selections );
  }

  private void addPastedElementsIntoDiagram() {
    if ( !isCopiedFromClipboard )
      readFromClipboard();
    int max = pastedElements.size();
    IDiagramLayer liveLayer = editor.getSource().getContext();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      IGraphicElement elem = (IGraphicElement) pastedElements.get( cntr );
      elem.setLayer( liveLayer, true );
    }
  }

  private void removePastedElementsFromDiagram() {
    int max = pastedElements.size();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      IGraphicElement elem = (IGraphicElement) pastedElements.get( cntr );
      elem.remove();
    }
  }

  private void readFromClipboard() throws IllegalArgumentException {
    PasteDiagramLayer pasteLayer = new PasteDiagramLayer();
    PainterManager.registerOwner( pasteLayer );
    try {
      Reader rdr = ClipboardUtils.createClipboardReader();
      try {
        ElementSelectionReader esr = new ElementSelectionReader( rdr );
        esr.readSelection( pasteLayer );
      } finally {
        rdr.close();
      }
    } catch ( Exception err ) {
      err.printStackTrace();
      throw new IllegalArgumentException( err.getMessage() );
    } finally {
      PainterManager.unregisterOwner( pasteLayer );
    }
    isCopiedFromClipboard = true;
  }

  public class PasteDiagramLayer implements IDiagramLayer {
    // IDiagramLayer
    public IGraphicElement getElementAtPoint( Point target ) { return null; }
    public IGraphicElement getElementAtPoint( Point target, IGraphicElement otherThan ) { return null; }
    public void getElementWithinLasso( Rectangle target, java.util.List store ) {
    	// nothing to do.
    }

    public IDiagram getDiagram() { return null; }

    public void setDirty() {
    	// nothing to do.
    }
    
    public void dispose() {
    	// nothing to do.
    }

    public void addElement( IGraphicElement toAdd ) {
      pastedElements.add( toAdd );
    }

    public void removeElement( IGraphicElement toRemove ) {
      pastedElements.remove( toRemove );
    }

    public void getElements( java.util.List store ) {
      store.addAll( pastedElements );
    }

    public int  getElementCount() {
      return pastedElements.size();
    }

    public void getElementsWithReferenceIds( java.util.Map idStore ) {
    	// nothing to do.
    }

    public void toBack( IGraphicElement toChg ) {
    	// nothing to do.
    }
    
    public void toFront( IGraphicElement toChg ) {
    	// nothing to do.
    }
    
    public void moveForward( IGraphicElement toChg ) {
    	// nothing to do.
    }
    
    public void moveBackward( IGraphicElement toChg ) {
    	// nothing to do.
    }

    public void beginUpdate() {
    	// nothing to do.
    }
    
    public void endUpdate() {
    	// nothing to do.
    }

    // ILayer
    public void paint( Graphics surface ) {
    	// nothing to do.
    }
    
    public void clear() {
    	// nothing to do.
    }

    public boolean isVisible() { return false; }
    public void setVisible( boolean show ) {
    	// nothing to do.
    }

    // IPersistentElement
    public void readFrom( Node self, HashMap idObjLookUp ) throws DOMException {
    	// nothing to do.
    }
    
    public void writeTo( Document doc, Element self, HashMap objIdLookup ) throws DOMException {
    	// nothing to do.
    }
  }
}